package com.tanhua.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mapper.UserInfoMapper;
import com.tanhua.model.db.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息服务接口实现类
 */
@DubboService //发布服务
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 用户注册：保存用户信息
     *
     * @param userInfo
     */
    @Override
    public void add(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    /**
     * 更新用户信息
     *
     * @param userInfo
     */
    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    /**
     * 用户信息管理-查询用户资料
     *
     * @param userId
     * @return
     */
    @Override
    public UserInfo getUserInfo(Long userId) {
        return userInfoMapper.selectById(userId);
    }

    /**
     * 根据黑名单用户ids查询黑名单用户集合数据
     *
     * @param blackListIds
     * @return
     */
    @Override
    public List<UserInfo> findByBatchIds(List<Long> blackListIds) {
        return userInfoMapper.selectBatchIds(blackListIds);
    }

    /**
     * 根据推荐用户的ids  gender age 查询userInfo
     *
     * @param listUserIds
     * @return
     */
    @Override
    public Map<Long, UserInfo> findByIds(List<Long> listUserIds, UserInfo uc) {
        //1.定义Map<Long, UserInfo> 结果返回
        Map<Long, UserInfo> userInfoMap = new HashMap<>();
        //2.根据listUserids userInfoCondtion 进行条件查询
        //select *from tb_user_info where id  in (1,2,3) and age <20 and gender = 'woman'
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", listUserIds);
        if (null != uc) {
            if (!StringUtils.isEmpty(uc.getAge())) {
                queryWrapper.lt("age", uc.getAge());
            }
            if (!StringUtils.isEmpty(uc.getGender())) {
                queryWrapper.eq("gender", uc.getGender());
            }
            if (!StringUtils.isEmpty(uc.getNickname())) {
                queryWrapper.like("nickname", uc.getNickname());
            }
        }
        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
        //3.封装结果返回
        for (UserInfo userInfo : userInfoList) {
            Long id = userInfo.getId();
            userInfoMap.put(id, userInfo);
        }
        return userInfoMap;
    }

    //根据当前用户id返回当前用户的同性的Ids
    @Override
    public List<Long> findSexAgeIds(Long userId) {
        //得到当前用户年龄和性别
        UserInfo userInfo = getUserInfo(userId);
        Integer age = userInfo.getAge();
        String gender = userInfo.getGender();

        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        //同性别的所有用户或者年龄大于或者小于3岁的用户
        queryWrapper.eq("gender",gender).or().gt("age",age+3).or().lt("age",age-3);
        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
        List<Long> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(userInfoList)) {
            for (UserInfo info : userInfoList) {
                list.add(info.getId());
            }
        }
        return list;
    }
}
